cmake_minimum_required(VERSION 3.10)
project(inudev_ros_nodelet)

SET(CMAKE_CXX_FLAGS "$ENV{CXXFLAGS} -O3 -Wall -g2 -ggdb")
SET(CMAKE_C_FLAGS "$ENV{CXXFLAGS} -O3 -Wall")
#set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_NO_SYSTEM_FROM_IMPORTED ON)

set( CMAKE_CXX_STANDARD 14)

add_definitions(-D_GE428_)
add_definitions(-D_GE430_)

SET(USE_PC_FILTER false CACHE BOOL "whether using point cloud filter")
if (USE_PC_FILTER)
   add_definitions(-DUSE_PC_FILTER)
endif()


## Paths for InuDev components. May be overriden externally.

if(DEFINED ENV{INUITIVE_STREAMS_INCLUDE})
  set(INUITIVE_STREAMS_LIBS $ENV{INUITIVE_STREAMS_LIBS})
  set(INUITIVE_STREAMS_INCLUDE $ENV{INUITIVE_STREAMS_INCLUDE})
  set(INUITIVE_COMMON_INCLUDE $ENV{INUITIVE_COMMON_INCLUDE})
  set(INUITIVE_BIN $ENV{INUITIVE_BIN})
else()
  set(INUITIVE_STREAMS_LIBS /opt/Inuitive/InuDev/lib)
  set(INUITIVE_STREAMS_INCLUDE /opt/Inuitive/InuDev/include)
  set(INUITIVE_COMMON_INCLUDE /opt/Inuitive/InuDev/include/Common)
  set(INUITIVE_BIN /opt/Inuitive/InuDev/bin)
endif()

## Compile as C++11, supported in ROS melodic and newer
#add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  cmake_modules
  camera_info_manager
  cv_bridge
  image_transport
  nodelet
  roscpp
  sensor_msgs
  std_msgs
  dynamic_reconfigure
  #vision_msgs
  message_generation
)

## Check if conan is arecognized command and if so, use it to install packages
if (USE_CONAN)
  include(conan/ConanCMakeLists.txt)
else() ## if conan not a command or didn't use var USE_CONAN, find packages locally
  ## System dependencies are found with CMake's conventions
  find_package(OpenCV REQUIRED COMPONENTS)
endif()

find_package(PCL REQUIRED)
#message(${PCL_INCLUDE_DIRS})
#set(my_pcl_libs pcl_common pcl_io pcl_filters pcl_io_ply pcl_kdtree pcl_octree pcl_segmentation)
#message(${PCL_LIBRARIES})

#find_package(pcl_conversions REQUIRED)
#message(${pcl_conversions_INCLUDE_DIRS})

##if(CATKIN_ENABLE_TESTING)
##  find_package(catkin REQUIRED COMPONENTS rostest )
##endif()

## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

find_library(InuSw_Streams_Lib REQUIRED NAMES InuStreams PATHS ${INUITIVE_STREAMS_LIBS})
find_library(InuSw_CommonUtils_Lib REQUIRED NAMES InuCommonUtilities PATHS ${INUITIVE_STREAMS_LIBS})

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a exec_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a exec_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to be processed
##   * uncomment the generate_messages entry below
##   * add every package in MSG_DEP_SET to generate_messages(DEPENDENCIES ...)

## Generate messages in the 'msg' folder
add_message_files(
  FILES
  features_msg.msg
)

## Generate services in the 'srv' folder
# add_service_files(
#   FILES
#   Service1.srv
#   Service2.srv
# )

## Generate actions in the 'action' folder
# add_action_files(
#   FILES
#   Action1.action
#   Action2.action
# )

## Generate added messages and services with any dependencies listed here
generate_messages(
  DEPENDENCIES
  std_msgs
)

################################################
## Declare ROS dynamic reconfigure parameters ##
################################################

## To declare and build dynamic reconfigure parameters within this
## package, follow these steps:
## * In the file package.xml:
##   * add a build_depend and a exec_depend tag for "dynamic_reconfigure"
## * In this file (CMakeLists.txt):
##   * add "dynamic_reconfigure" to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * uncomment the "generate_dynamic_reconfigure_options" section below
##     and list every .cfg file to be processed

## Generate dynamic reconfigure parameters in the 'cfg' folder
generate_dynamic_reconfigure_options(
   cfg/inudev_ros_nodelet.cfg
#   cfg/DynReconf2.cfg
)

###################################
## catkin specific configuration ##
###################################
## The catkin_package macro generates cmake config files for your package
## Declare things to be passed to dependent projects
## INCLUDE_DIRS: uncomment this if your package contains header files
## LIBRARIES: libraries you create in this project that dependent projects also need
## CATKIN_DEPENDS: catkin_packages dependent projects also need
## DEPENDS: system dependencies of this project that dependent projects also need
catkin_package(
  INCLUDE_DIRS include
  LIBRARIES inudev_ros_nodelet
  CATKIN_DEPENDS cv_bridge image_transport nodelet roscpp sensor_msgs std_msgs camera_info_manager 
  #  vision_msgs
  #  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
include_directories(
  include
  src/filter/
  ${OpenCV_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
  #${pcl_conversions_INCLUDE_DIRS}
  ${INUITIVE_STREAMS_INCLUDE}
  ${INUITIVE_COMMON_INCLUDE}
  ${catkin_INCLUDE_DIRS}
  ${dynamic_reconfigure_PACKAGE_PATH}/cmake/cfgbuild.cmake
)

set(src_filter
  src/filter/depth2pc.cpp
  src/filter/space_filter.cpp
  src/filter/temporal_filter.cpp
)

if (USE_PC_FILTER)
set(src_filter
  ${src_filter}
  src/filter/pc_filter.cpp
)
endif()

message(${src_filter})
## Declare a C++ library
add_library(${PROJECT_NAME} SHARED
  ${src_filter}
  src/ros_pointcloud_publisher.cpp
  src/inudev_ros_nodelet.cpp
  src/ros_imu_publisher.cpp
  src/ros_disparity_publisher.cpp
  src/ros_depth_publisher.cpp
  src/ros_webcam_publisher.cpp
  src/ros_video_publisher.cpp
  src/ros_publisher.cpp
  src/ros_sensor.cpp
  src/ros_publisher_bookkeeping.cpp
  src/ros_video_publishers_base.cpp
  src/ros_fisheye_publisher.cpp
  #src/ros_slam_publisher.cpp
  src/NotImplementedException.cpp
  #src/ros_objectdetection_publisher.cpp
  src/ros_features_publisher.cpp
)

## Add cmake target dependencies of the library
## as an example, code may need to be generated before libraries
## either from message generation or dynamic reconfigure
# add_dependencies(${PROJECT_NAME} ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Declare a C++ executable
## With catkin_make all packages are built within a single CMake context
## The recommended prefix ensures that target names across packages don't collide
# add_executable(${PROJECT_NAME}_node src/inudev_ros_nodelet_node.cpp)

## Rename C++ executable without prefix
## The above recommended prefix causes long target names, the following renames the
## target back to the shorter version for ease of user use
## e.g. "rosrun someones_pkg node" instead of "rosrun someones_pkg someones_pkg_node"
# set_target_properties(${PROJECT_NAME}_node PROPERTIES OUTPUT_NAME node PREFIX "")

## Add cmake target dependencies of the executable
## same as for the library above
# add_dependencies(${PROJECT_NAME}_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})

## Specify libraries to link a library or executable target against
target_link_libraries(${PROJECT_NAME}
  ${OpenCV_LIBRARIES}
  ${PCL_LIBRARIES}
  #${my_pcl_libs}
  ${InuSw_Streams_Lib}
  ${InuSw_CommonUtils_Lib}
  -lstdc++fs
  ${catkin_LIBRARIES}
  #${INUITIVE_BIN}/libopencv_highgui.so.4.5
  #${INUITIVE_BIN}/libopencv_imgcodecs.so.4.5
  #${INUITIVE_BIN}/libopencv_imgproc.so.4.5
  #${INUITIVE_BIN}/libopencv_core.so.4.5
)

add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_gencfg ${PROJECT_NAME}_generate_messages_cpp)

#############
## Install ##
#############

# all install targets should use catkin DESTINATION variables
# See http://ros.org/doc/api/catkin/html/adv_user_guide/variables.html

## Mark executable scripts (Python etc.) for installation
## in contrast to setup.py, you can choose the destination
#install(PROGRAMS
#   scripts/my_python_script
#  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
#)

## Mark executables and/or libraries for installation
#install(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_node
install(TARGETS ${PROJECT_NAME}
   ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
   RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

## Mark cpp header files for installation
# install(DIRECTORY include/${PROJECT_NAME}/
#   DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
#   FILES_MATCHING PATTERN "*.h"
#   PATTERN ".svn" EXCLUDE
# )

## Mark other files for installation (e.g. launch and bag files, etc.)
install(FILES
    launch/inudev_ros_nodelet_all_transports_disable.launch
    launch/inudev_ros_nodelet.launch
    launch/inudev_ros_nodelet_transport_disable.launch
    launch/inudev_ros_nodelet_multiple.launch
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
)

install(FILES
    nodelet_plugins.xml
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}
)

install(FILES
    ${InuSw_Streams_Lib}
    DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)

install(FILES
    ${InuSw_CommonUtils_Lib}
    DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
)

install(FILES
    cfg/InuRosParams.yaml
    cfg/InuRosParams1.yaml
    cfg/InuRosParams2.yaml
    cfg/InuRosParams3.yaml
    cfg/InuRosParams4.yaml
    cfg/InuRosParams5.yaml
    cfg/InuSW_Version.txt
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/cfg
)

#install(CODE [[
#    file(GET_RUNTIME_DEPENDENCIES
#        RESOLVED_DEPENDENCIES_VAR RES
#        UNRESOLVED_DEPENDENCIES_VAR UNRES
#        LIBRARIES $<TARGET_FILE:inudev_ros_nodelet>
#    )
#
#    list(FILTER RES INCLUDE REGEX ".+libopencv_.+")
#
#    file(INSTALL
#        DESTINATION "${CMAKE_INSTALL_PREFIX}/lib"
#        TYPE SHARED_LIBRARY
#        FOLLOW_SYMLINK_CHAIN
#        FILES ${RES}
#    )
#   ]]
#)

#############
## Testing ##
#############

# if (CATKIN_ENABLE_TESTING)
#   ## Add gtest based cpp test target and link libraries
#   catkin_add_gtest(${PROJECT_NAME}-test test/test_inudev_ros_nodelet.cpp)
#   if(TARGET ${PROJECT_NAME}-test)
#     target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
#   endif()
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)

#############
## Custom  ##
#############
# add_custom_command(TARGET ${PROJECT_NAME}
#     POST_BUILD
#     COMMAND pwd
#     COMMAND [ -d doc ] || mkdir doc
#     COMMAND echo "Running Doxygen"
#     COMMAND doxygen ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile > doc/doxygen.log.txt
#     VERBATIM
# )

# if(EXISTS /usr/bin/latex)
#   add_custom_command(TARGET ${PROJECT_NAME}
#       POST_BUILD
#       DEPENDS doc/latex/Makefile
#       COMMAND echo "Running Latex"
#       COMMAND make -C doc/latex > doc/latex.log.txt < /dev/null
#       VERBATIM
#   )
# else()
#   add_custom_command(TARGET ${PROJECT_NAME}
#       POST_BUILD
#       COMMAND echo "Warning: Skipping refman.pdf creation due to latex avalibility"
#       VERBATIM
#   )
# endif()
